<?php
$sql = "SELECT * FROM #__categories WHERE published=1 AND section=1 AND parent_id=0 ORDER BY ordering ASC,id DESC";
$database->setQuery($sql);
$listCategories = $database->loadObjectList();
?>
<div id="menu">
	<ul id="toplevel">
		<?php
		for ($i=0; $i<count($listCategories); $i++){
			$link = sefRelToAbs("index.php?option=com_content&task=blogcategory&id=".$listCategories[$i]->id."-".$listCategories[$i]->name);
			if($i==(count($listMenu)-1)){
		 		$style = ' class="last_menu"';
		 	}else{
		 		$style = '';
		 	}
		 	/*-- menu-submenu --*/
			$sql = "select * from #__categories where parent_id=".$listCategories[$i]->id;
			$database->setQuery($sql);
			$lsRows = $database->loadObjectList();
			if (count($lsRows)>0){
				$array = "(".$listCategories[$i]->id.",";
				for ($j=0; $j<count($lsRows); $j++){
					if ($j==(count($lsRows)-1)){
						$str = "";
					}else{
						$str = ",";
					}
					$array .= $lsRows[$j]->id.$str;
				}
				$array .= ")";
				//echo $array;
				$whereMenuLeft = " AND catid IN".$array;
			}else{
				$whereMenuLeft = " AND catid=".$listCategories[$i]->id;
			}
			
			$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND sectionid=1".$whereMenuLeft;
			$database->setQuery($sql);
			$total = $database->loadResult();
			
			$sql = "SELECT count(*) FROM #__categories WHERE published=1 AND section=1 AND parent_id=".$listCategories[$i]->id;
			$database->setQuery($sql);
			$totalSubmenu = $database->loadResult();
			$sql = "SELECT * FROM #__categories WHERE published=1 AND section=1 AND parent_id=".$listCategories[$i]->id." ORDER BY ordering ASC, id DESC";
			$database->setQuery($sql);
			$listSubmenu = $database->loadObjectList();
			if($totalSubmenu>0){
				?>
				<li id="p<?php echo $listCategories[$i]->id;?>"><a href="<?php echo $link;?>" class="time fly" begin="p<?php echo $listCategories[$i]->id;?>.mouseenter" end="p<?php echo $listCategories[$i]->id;?>.mouseleave" timeaction="class:p1Hover"><?php echo $listCategories[$i]->title;?></a>
					<ul class="time" begin="p<?php echo $listCategories[$i]->id;?>.mouseenter" end="p<?php echo $listCategories[$i]->id;?>.mouseleave" timeaction="display">
						<?php
						for ($j=0; $j<count($listSubmenu); $j++){
							$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND sectionid=1 AND catid=".$listSubmenu[$j]->id;
							$database->setQuery($sql);
							$totalSub = $database->loadResult();	
							$linkSub = sefRelToAbs("index.php?option=com_content&task=blogcategory&id=".$listSubmenu[$j]->id."-".$listSubmenu[$j]->name);
							
							$sql = "SELECT count(*) FROM #__categories WHERE published=1 AND section=1 AND parent_id=".$listSubmenu[$j]->id;
							$database->setQuery($sql);
							$totalSubmenu2 = $database->loadResult();
							$sql = "SELECT * FROM #__categories WHERE published=1 AND section=1 AND parent_id=".$listSubmenu[$j]->id." ORDER BY ordering ASC, id DESC";
							$database->setQuery($sql);
							$listSubmenu2 = $database->loadObjectList();	
							//if($totalSubmenu2>0){
							?>	
							<li id="p<?php echo $listSubmenu[$j]->id;?>"><a href="<?php echo $linkSub;?>"><?php echo $listSubmenu[$j]->title;?></a>
								<ul class="time" begin="p<?php echo $listSubmenu[$j]->id;?>.mouseenter" end="p<?php echo $listSubmenu[$j]->id;?>.mouseleave" timeaction="display">
									<?php
									for ($k=0; $k<count($listSubmenu2); $k++){
										$sql = "SELECT COUNT(*) FROM #__content WHERE state=1 AND sectionid=1 AND catid=".$listSubmenu2[$k]->id;
										$database->setQuery($sql);
										$totalSub2 = $database->loadResult();	
										$linkSub2 = sefRelToAbs("index.php?option=com_content&task=blogcategory&id=".$listSubmenu2[$k]->id."-".$listSubmenu2[$k]->name);
										?>
										<li id="p<?php echo $listSubmenu2[$k]->id;?>"><a href="<?php echo $linkSub2;?>"><?php echo $listSubmenu2[$k]->title;?></a></li>
										<?php
									}
									?>
								</ul>
							</li>	
							<?php 
							//}
						}	
						?>
					</ul>
				</li>
				<?php
			}else{
				?>
				<li><a href="<?php echo $link;?>"><?php echo $listCategories[$i]->title;?></a></li>
				<?php
			}
		}
		?>
	</ul>
</div>